<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\DB;

/**
 * 供应商
 *
 * @icon fa fa-circle-o
 */
class Supplier extends Backend
{
    
    /**
     * Supplier模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Supplier');
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("stateList", $this->model->getStateList());
    }

    // 场地商
    public function site()
    {
      //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where('type','1')
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where('type','1')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch('index');

    }

    // 活动商
    public function activity()
    {
      //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where('type','2')
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where('type','2')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch('index');

    }

    // 主讲老师
    public function master()
    {
      //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->where('type','3')
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where($where)
                    ->where('type','3')
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch('index');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
    * 导出功能
    */
    public function export()
    {
        $supplier = $this->model->select();
        $title = "供应商列表";
        $path = "uploads/EXCEL/"; //文件保存路径
        $PHPExcel = new \PHPExcel(); 
        //获得当前活动sheet的操作对象
        $PHPSheet = $PHPExcel->getActiveSheet(); 
        //给当前活动sheet设置名称
        $PHPSheet->setTitle($title);
        $PHPSheet->setCellValue('A1', '名称');
        $PHPSheet->setCellValue('B1', '联系方式');
        $PHPSheet->getColumnDimension('A')->setWidth(25);
        $PHPSheet->getColumnDimension('B')->setWidth(25);
    
        $hang = 2;
        foreach($supplier as $k => $v)
        {
            $PHPSheet->setCellValue('A'.$hang, $v['name']);
            $PHPSheet->setCellValue('B'.$hang, $v['mobile']);
            $hang = $hang + 1;
        }
                // Redirect output to a client’s web browser (Excel2007)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="供应商列表'.time().'.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); 
        //按照指定格式生成Excel文件，‘Excel2007’表示生成2007版本的xlsx，
        $file = date('Ymdhi',time()).'.xlsx';
        $PHPWriter->save($path.$file); //表示在$path路径下面生成demo.xlsx文件*/
        $PHPWriter->save('php://output');
    }

    /**
     * 供应商详情，根据身份不同显示不同的内容
     */
    public function detail($ids)
    {
        $type = $this->model::where('id', $ids)->column('type');
        if($type[0] == 1) // 场地商
        {
            $row = DB::name('supplier')
                    ->field('avatar,mobile,name,city,address,person,sitetype,truename,company_post,company_name,company_addr,service')
                    ->where('id',$ids)
                    ->find();
            $sitetype =explode(',', $row['sitetype']);
            $sitetypeString = '';
            foreach ($sitetype as $key => $value) {
               switch ($value) {
                   case '1':
                       $sitetypeString .= '  商务会议';
                       break;
                   case '2':
                       $sitetypeString .= '  酒店宴席';
                       break;
                   case '3':
                       $sitetypeString .= '  专业会议策划';
                       break;
                   case '4':
                       $sitetypeString .= '  教育培训';
                       break;
                   case '5':
                       $sitetypeString .= '  公司年会';
                       break;
                   default:
                       $sitetypeString .= '  广告发布';
                       break;
               }
            }
           $row['sitetype'] = $sitetypeString;
           $row['avatar'] = '<img src="'.$row['avatar'].'" style="width:100px; height:100px;"/>';
        }
        else if($type[0] == 2) // 活动方
        {
            $row = DB::name('supplier')
                    ->field('avatar,mobile,name,city,address,person,sitetype,truename,company_post,company_name,company_addr,service,is_ticket')
                    ->where('id',$ids)
                    ->find();
            $sitetype =explode(',', $row['sitetype']);
            $sitetypeString = '';
            foreach ($sitetype as $key => $value) {
               switch ($value) {
                   case '1':
                       $sitetypeString .= '  商务会议';
                       break;
                   case '2':
                       $sitetypeString .= '  酒店宴席';
                       break;
                   case '3':
                       $sitetypeString .= '  专业会议策划';
                       break;
                   case '4':
                       $sitetypeString .= '  教育培训';
                       break;
                   case '5':
                       $sitetypeString .= '  公司年会';
                       break;
                   default:
                       $sitetypeString .= '  广告发布';
                       break;
               }
            }
           $row['sitetype'] = $sitetypeString;
           $row['is_ticket'] = $row['is_ticket'] == 1?"是":"否";
           $row['avatar'] = '<img src="'.$row['avatar'].'" style="width:100px; height:100px;"/>';
        }
        else
        {
            // 主讲老师
            $row = DB::name('supplier')
                    ->field('avatar,mobile,name,city,office,company_post,honor,selfdesc')
                    ->where('id',$ids)
                    ->find();
            $row['avatar'] = '<img src="'.$row['avatar'].'" style="width:100px; height:100px;"/>';
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
